import { createRouter, RouteRecordRaw, createWebHashHistory } from "vue-router";
import { loading } from '@/utils/vnode'
import pageRouter from './page'
import viewRouter from './view'
import { formatRoutes } from "@/utils/dealRouter";
import { useStore } from "../store/index";
import { storeToRefs } from 'pinia';

// 生成路由 信息
declare module 'vue-router' {
    interface RouteMeta {
        title: string,
        // transition: string,
        // keepAlive:boolean
    }
}

// 创建路由实列
const router = createRouter({
    history: createWebHashHistory(), //创建hash 路由模式
    // routes: routes
    routes: [...pageRouter, ...viewRouter]
})
// 声名白名单 但是弃用
// const whiteList = ['/']
let load = 0
router.beforeEach((to, from, next) => {
    loading.component?.exposed?.startLoading()
    console.log(to.path, 'path');
    const store = useStore();
let {editableTabs,count,editableTabsValue}=storeToRefs(store)
    // console.log(router.getRoutes(),'222');
    // 在这里进行判断是否未登录 
    if (load == 0&& localStorage.getItem("viteMenu")) {
        
        load++
        let arr: Array<object> = JSON.parse(
            localStorage.getItem("viteMenu") as string
        );
        let route: Array<object> = formatRoutes(arr, true) as [];
        route.forEach((item: any, idx: number) => {
            // console.log(idx);
            router.addRoute(item);
        });
        if(localStorage.getItem("editableTabs")){
            editableTabsValue.value=JSON.parse(localStorage.getItem('editableTabsValue') as '')
            editableTabs.value=JSON.parse(localStorage.getItem('editableTabs')as '' )
        }
      
        // console.log(router.getRoutes(), '2');
        next({path: to.path})
    }else{
        next()
    }

    
})
router.afterEach((to, from) => {
    loading.component?.exposed?.endLoading()
})
// 导出
export default router